using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json;

namespace BaseAmd.BaseData
{
	public class BaseRtIntegrateEntity
	{
		public List<BaseAxisInfoEntity> AxisInfo { get; set; } = new List<BaseAxisInfoEntity>();


		public BaseCncStateEntity CncStateEntity { get; set; }

		public BaseOvrEntity OvrInfo { get; set; }

		public BaseProgInfoNow ProgInfoNow { get; set; }

		public BaseTimePartCount TimePartCount { get; set; }

		public Dictionary<string, object> ConvertToDic(Dictionary<string, object> dicExt = null)
		{
			Dictionary<string, object> dic = new Dictionary<string, object>();
			this.ConvertToDic("axis", this.AxisInfo, ref dic);
			this.ConvertToDic(this.CncStateEntity, ref dic);
			this.ConvertToDic(this.OvrInfo, ref dic);
			this.ConvertToDic(this.ProgInfoNow, ref dic);
			this.ConvertToDic(this.TimePartCount, ref dic);
			if (dicExt != null)
			{
				foreach (string key in dicExt.Keys)
				{
					if (!dic.ContainsKey(key))
					{
						dic.Add(key, dicExt[key]);
					}
				}
			}
			return dic;
		}

		private void ConvertToDic<T>(string keyName, List<T> entities, ref Dictionary<string, object> dic) where T : class
		{
			List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
			foreach (T entity in entities)
			{
				Dictionary<string, object> dic2 = new Dictionary<string, object>();
				this.ConvertToDic(entity, ref dic2);
				list.Add(dic2);
			}
			string value = JsonConvert.SerializeObject(list);
			dic.Add(keyName, value);
		}

		private void ConvertToDic<T>(T entity, ref Dictionary<string, object> dic) where T : class
		{
			if (entity == null)
			{
				return;
			}
			PropertyInfo[] properties = entity.GetType().GetProperties();
			PropertyInfo[] array = properties;
			foreach (PropertyInfo propertyInfo in array)
			{
				if (!((Attribute.GetCustomAttribute(propertyInfo, typeof(AcqVarAttribute), inherit: false) as AcqVarAttribute)?.Ignore ?? false) && !dic.ContainsKey(propertyInfo.Name))
				{
					dic.Add(propertyInfo.Name, propertyInfo.GetValue(entity));
				}
			}
		}
	}
}
